<?

class Mdl_Pages extends MY_Model {

    function Mdl_Pages() {

        parent::MY_Model();

        $this->limit = 25;

    }

    function get($params = NULL) {

        $this->prep_params($params);

        if (isset($params['page_id'])) {

            $this->db->where('page_id', $params['page_id']);

        }

        if (isset($params['url_title'])) {

            $this->db->where('url_title', $params['url_title']);

        }

        if (isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $offset = ($this->page * $this->limit) - $this->limit;

            $this->db->limit($this->limit, $offset);

        }

        $this->db->join('inktype_users', 'inktype_users.user_id = inktype_pages.user_id');

        if ($this->is_admin) {

            $this->db->where('inktype_pages.site_id', $this->session->userdata('site_id'));

        }

        else {

            $this->db->where('inktype_pages.site_id', $this->inktype->site_id);

        }

        $query = $this->db->get('inktype_pages');

        if (isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $this->set_page_links();

        }

        foreach ($query->result() as $page) {

            unset ($page->password, $page->user_type_id, $page->last_login, $page->date_registered, $page->website_url);

        }

        return $query->result();

    }

    function save() {

        $db_array = $this->prep_db_array();

        $db_array['site_id'] = $this->session->userdata('site_id');

        if (!isset($db_array['allow_comments'])) {

            $db_array['allow_comments'] = 0;

        }

        if (!isset($db_array['published'])) {

            $db_array['published'] = 0;

        }

        $db_array['url_title'] = url_title($db_array['title']);

        if (!isset($this->uri_assoc['page_id'])) {

            $time = time();

            $db_array['date_created'] = $time;

            $db_array['date_updated'] = $time;

            $this->db->insert('inktype_pages', $db_array);

        }

        else {

            $this->db->where('page_id', $this->uri_assoc['page_id']);

            $this->db->update('inktype_pages', $db_array);

        }

        $this->session->set_flashdata('success_save', TRUE);

    }

    function delete() {

        if (isset($this->uri_assoc['page_id'])) {

            $this->db->where('page_id', $this->uri_assoc['page_id']);

            $this->db->delete('inktype_pages');

            $this->session->set_flashdata('success_delete', TRUE);

        }

    }

    function validate() {

        $this->fields = array(
            'title'             =>  'Title',
            'url_title'         =>  'URL Title',
            'content'           =>  'Content',
            'date_created'      =>  'Date Created',
            'date_updated'      =>  'Date Updated',
            'user_id'           =>  'Author',
            'published'         =>  'Published',
            'display_order'     =>  'Display Order',
            'allow_comments'    =>  'Allow Comments'
        );

        $this->rules = array(
            'title'             =>  'required',
            'content'           =>  'required',
            'user_id'           =>  'required',
            'display_order'     =>  'required'
        );

        return $this->_validate();

    }

}

?>